package com.zhf.controller;

import com.zhf.domain.Course;
import com.zhf.domain.CourseVO;
import com.zhf.domain.ResponseResult;
import com.zhf.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: ZhaoHuanfeng
 * @Date: 2020/12/2 19:59
 */
@RestController
@RequestMapping("/course")
public class CourseController {

    @Autowired
    private CourseService courseService;

    /*
    多条件查询课程列表
     */
    @RequestMapping("/findCourseByCondition")
    public ResponseResult findCourseByCondition(@RequestBody CourseVO courseVO){
        List<Course> list = courseService.findCourseByCondition(courseVO);
        ResponseResult result = new ResponseResult(true,200,"响应成功",list);
        return result;
    }

    /*
    课程图片上传
     */
    @RequestMapping("/courseUpload")
    public ResponseResult fileUpload(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws IOException {
        //判断接收到的上传文件是否为空
        if (file.isEmpty()){
            throw new RuntimeException();
        }
        //获取项目的部署路径
        String realPath = request.getServletContext().getRealPath("/");
        String ssm_web = realPath.substring(0, realPath.indexOf("ssm-web"));

        //获取源文件名
        String filename = file.getOriginalFilename();

        //生成新文件名
        String newFileName = System.currentTimeMillis() + filename.substring(filename.lastIndexOf("."));

        //文件上传
        String uploadPath=ssm_web+"upload\\";
        File filePath = new File(uploadPath, newFileName);
        //如果不存在则创建目录
        if(!filePath.getParentFile().exists()){
            filePath.getParentFile().mkdirs();
            System.out.println("创建目录："+filePath);
        }
        //图片上传
        file.transferTo(filePath);

        //将文件名和文件路径返回，进行响应
        HashMap<String, String> map = new HashMap<>();
        map.put("fileName", newFileName);
        map.put("filePath","http://localhost:8080/upload/"+newFileName);
        ResponseResult result = new ResponseResult(true, 200, "图片上传成功", map);
        return result;
    }

    /*
    新增课程信息及讲师信息
    新增课程信息和修改课程信息要写在同一个方法中
     */
    @RequestMapping("/saveOrUpdateCourse")
    public ResponseResult saveOrUpdateCourse(@RequestBody CourseVO courseVO) throws InvocationTargetException, IllegalAccessException {
        if(courseVO.getId()==null){
            courseService.saveCourseOrTeacher(courseVO);

            ResponseResult result = new ResponseResult(true, 200, "新增成功", null);
            return result;
        }else{
            courseService.updateCourseOrTeacher(courseVO);

            ResponseResult result = new ResponseResult(true, 200, "修改成功", null);
            return result;
        }
    }

    /*
    根据id获取课程信息及讲师信息
     */
    @RequestMapping("/findCourseById")
    public ResponseResult findCourseById(Integer id){
        CourseVO courseVO = courseService.findCourseById(id);
        ResponseResult result = new ResponseResult(true, 200, "根据id查询课程信息成功", courseVO);
        return result;
    }

    /*
    课程状态管理
     */
    @RequestMapping("/updateCourseStatus")
    public ResponseResult updateCourseStatus(int id,int status){
        courseService.updateCourseStatus(id, status);
        HashMap<String, Integer> map = new HashMap<String, Integer>();
        map.put("status", status);
        ResponseResult result = new ResponseResult(true, 200,"状态修改成功",map);
        return result;
    }
}
